package com.hsav.kcxb.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hsav.kcxb.R;
import com.hsav.kcxb.activity.RecordActivity;
import com.hsav.kcxb.activity.UnisonListActivity;
import com.hsav.kcxb.main.MSYS;
import com.kcbsdk.MyRankBa.AObbligatoList;
import com.zhy.autolayout.utils.AutoUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import main.MAPI;

/**
 * Created by zhao on 2017/2/17.
 */

public class SingerWorkAdapter extends BaseAdapter implements View.OnClickListener{

    private Context mContext;
    private LayoutInflater mInflater;
    private List<AObbligatoList> data;

    public SingerWorkAdapter(Context context){
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
    }

    public void setData(List<AObbligatoList> data)
    {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (data == null) return 0;
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        if (data == null) return null;
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView==null){
            convertView = mInflater.inflate(R.layout.item_singer_work,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.imgHead = (ImageView) convertView.findViewById(R.id.img_head);
            viewHolder.textSongName = (TextView)convertView.findViewById(R.id.text_song_name);
            viewHolder.textSongSize =(TextView)convertView.findViewById(R.id.text_song_size);
            viewHolder.textSingerName = (TextView) convertView.findViewById(R.id.text_singer_name);
            viewHolder.textToSing = (TextView) convertView.findViewById(R.id.text_to_sing);
            viewHolder.textToSing.setOnClickListener(this);
            viewHolder.viewDummy = convertView.findViewById(R.id.view_dummy);
            viewHolder.viewDummy.setOnClickListener(this);
            convertView.setTag(viewHolder);
            AutoUtils.autoSize(convertView);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (data != null)
        {
            AObbligatoList item = data.get(position);
            viewHolder.viewDummy.setTag(position);
            viewHolder.textToSing.setTag(position);
            if (item.IconFile != null && !"".equals(item.IconFile) && new File(item.IconFile).exists())
                viewHolder.imgHead.setImageURI(Uri.fromFile(new File(item.IconFile)));
            viewHolder.textSongName.setText(item.SongName);
            viewHolder.textSongSize.setText(String.format("%.2fM", item.FileSize / 1000 / 1024.0f));
            viewHolder.textSingerName.setText(item.ArtistName);
        }

        return convertView;
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        Intent intent;
        switch (v.getId()){
            case R.id.view_dummy:
                intent = new Intent(mContext, UnisonListActivity.class);
                mContext.startActivity(intent);
                Toast.makeText(mContext, "name="+position, Toast.LENGTH_SHORT).show();
                break;
            case R.id.text_to_sing:
                intent = MSYS.startRecordActivity(mContext, data.get(position));
                mContext.startActivity(intent);
                // mContext.startActivity(new Intent(mContext, RecordActivity.class));
//                Toast.makeText(mContext, "sing="+position, Toast.LENGTH_SHORT).show();
                break;
        }

    }

    private class ViewHolder{
        ImageView imgHead;
        TextView textSongName;
        TextView textSongSize;
        TextView textSingerName;
        TextView textToSing;

        View viewDummy;
    }
}
